# import tkinter as tk
import os
import pyaudio
import wave
import time
import threading


class Alarm:
    def __init__(self, alarm_file):
        # self.search_first_device()
        self.device_index = 19
        self.alarm_file = alarm_file
        self.alarm = False
        self.alarm_status = "状态正常，未报警"
        # self.root = tk.Tk()
        # self.root.title("报警器")
        # self.root.geometry("400x400")
        # self.root.resizable(False, False)
        # self.root.protocol("WM_DELETE_WINDOW", self.on_close)
        # self.status_label = tk.Label(self.root, text=self.alarm_status)
        # self.status_label.pack()
        # self.start_button = tk.Button(
        #     self.root, text="开始报警", command=self.start_alarm)
        # self.start_button.pack()
        # self.stop_button = tk.Button(
        #     self.root, text="停止报警", command=self.stop_alarm)
        # self.stop_button.pack()

    def search_first_device(self):
        self.p = pyaudio.PyAudio()
        self.device_index = None
        info = self.p.get_host_api_info_by_index(0)
        numdevices = info.get('deviceCount')
        for i in range(0, numdevices):
            if (self.p.get_device_info_by_host_api_device_index(0, i).get('maxOutputChannels')) > 0:
                self.device_index = i
                break

    # def update_status_label(self):
        # self.status_label.config(text=self.alarm_status)

    def start_alarm(self):
        self.alarm = True
        # self.alarm_status = "状态异常，正在报警"
        # self.update_status_label()
        # self.start_button.config(state=tk.DISABLED)
        threading.Thread(target=self.process_alarm).start()

    def process_alarm(self):
        self.p = pyaudio.PyAudio()
        self.wf = wave.open(self.alarm_file, 'rb')
        self.stream = self.p.open(format=self.p.get_format_from_width(self.wf.getsampwidth()),
                                  channels=self.wf.getnchannels(),
                                  rate=self.wf.getframerate(),
                                  output=True,
                                  output_device_index=self.device_index)
        while self.alarm:
            data = self.wf.readframes(1024)
            if data == b'':
                self.wf.rewind()
                data = self.wf.readframes(1024)
            self.stream.write(data)
        self.stream.stop_stream()
        self.stream.close()
        self.p.terminate()
        self.wf.close()

    def stop_alarm(self):
        self.alarm = False
        # self.alarm_status = "状态正常，未报警"
        # self.start_button.config(state=tk.NORMAL)
        # self.update_status_label()

    def on_close(self):
        self.stop_alarm()
        # self.root.destroy()

    # def alarm_gui(self):
    #     self.root.mainloop()


def AlarmController(Alarm: Alarm, alarmtime):
    Alarm.start_alarm()
    time.sleep(alarmtime)
    Alarm.stop_alarm()


if __name__ == '__main__':
    alarm = Alarm(os.path.join(os.path.dirname(__file__), "./alarm.wav"))
    # alarm.device_index = 3
    threading.Thread(target=AlarmController, args=(alarm,)).start()
    # alarm.alarm_gui()
